<?php
// +---------------------------------------------------------------------+
// | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]                |
// +---------------------------------------------------------------------+
// | Copy right 2019-2029 www.niucloud.com                          |
// +---------------------------------------------------------------------+
// | Author | NiuCloud <niucloud@outlook.com>                       |
// +---------------------------------------------------------------------+
// | Repository | https://github.com/niucloud/framework.git          |
// +---------------------------------------------------------------------+

namespace addon\wechatpay\model;

use app\model\system\Config as ConfigModel;
use app\model\BaseModel;

/**
 * 微信支付配置
 * 版本 1.0.4
 */
class Config extends BaseModel
{


    CONST TRANSFER_V3_TYPE_SHOP = 1; //商户转账
    CONST TRANSFER_V3_TYPE_USER = 2; //会员收款

    /**
     * 设置支付配置
     * array $data
     */
    public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
    {
        $config = new ConfigModel();

        if(!($data['transfer_status'] == 1 && $data['transfer_type'] == 'v3' && $data['transfer_v3_type'] == self::TRANSFER_V3_TYPE_USER)){
            $data['member_withdraw_scene'] = '';
            $data['member_withdraw_code'] = '';
            $data['member_withdraw_info'] = [];
            $data['member_withdraw_recv'] = '';
        }

        $res = $config->setConfig($data, '微信支付配置', 1, [ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'WECHAT_PAY_CONFIG' ] ]);
        return $res;
    }

    /**
     * 获取支付配置
     */
    public function getPayConfig($site_id = 0, $app_module = 'shop')
    {
        $config = new ConfigModel();
        $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', $app_module ], [ 'config_key', '=', 'WECHAT_PAY_CONFIG' ] ]);
        $res['data']['value'] = array_merge([
            'api_type' => 'v2',
            'transfer_type' => 'v2',
            'transfer_v3_type' => '1',//旧版1 新版2
            'transfer_status'=>0,
            'pay_status'=>0,
            'refund_status'=>0,

            'member_withdraw_scene' => '',
            'member_withdraw_code' => '',
            'member_withdraw_info' => [],
            'member_withdraw_recv' => '',

        ], $res['data']['value']);

        return $res;
    }

    public function getTransferSceneConfig()
    {
        $config = [
            [
                'num'=>1,
                'title' => '现金营销',
                'infos' => [
                    [
                        'info_type' => '活动名称',
                        'info_content' => ''
                    ],
                    [
                        'info_type' => '奖励说明',
                        'info_content' => '',
                    ],
                ],
                'user_recv'=>[
                    '活动奖励',
                    '现金奖励'
                ]
            ],
            [
                'num'=>2,
                'title' => '企业赔付',
                'infos' => [
                    [
                        'info_type' => '赔付原因',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '退款',
                    '商家赔付'
                ]
            ],
            [
                'num'=>3,
                'title' => '佣金报酬',
                'infos' => [
                    [
                        'info_type' => '岗位类型',
                        'info_content' => ''
                    ],
                    [
                        'info_type' => '报酬说明',
                        'info_content' => '',
                    ],
                ],
                'user_recv'=>[
                    '劳务报酬',
                    '报销款',
                    '企业补贴',
                    '开工利是',
                ]
            ],
            [
                'num'=>4,
                'title' => '采购货款',
                'infos' => [
                    [
                        'info_type' => '采购商品名称',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '货款',
                ]
            ],
            [
                'num'=>5,
                'title' => '二手回收',
                'infos' => [
                    [
                        'info_type' => '回收商品名称',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '二手回收货款',
                ]
            ],
            [
                'num'=>6,
                'title' => '公益补助',
                'infos' => [
                    [
                        'info_type' => '公益活动名称',
                        'info_content' => ''
                    ],
                    [
                        'info_type' => '公益活动备案编号',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '公益补助金',
                ]
            ],
            [
                'num'=>7,
                'title' => '行政补贴',
                'infos' => [
                    [
                        'info_type' => '补贴类型',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '行政补贴',
                    '行政奖励',
                ]
            ],
            [
                'num'=>8,
                'title' => '保险理赔',
                'infos' => [
                    [
                        'info_type' => '保险产品备案编号',
                        'info_content' => ''
                    ],
                    [
                        'info_type' => '保险名称',
                        'info_content' => ''
                    ],
                    [
                        'info_type' => '保险操作单号',
                        'info_content' => ''
                    ],
                ],
                'user_recv'=>[
                    '保险理赔款',
                ]
            ]
        ];

        $data = [];
        foreach ($config as $k=>$v){
            $data[$v['num']] = $v;
        }
        return $data;
    }


    public function getTransferSceneInfo($param){

        $data = [
            'member_withdraw_info'=>[],
        ];

        if(!isset($param['transfer_v3_type']) || $param['transfer_v3_type'] !=2){
            return $data;
        }
        $config = $this->getTransferSceneConfig();
        //会员提现场景
        if(!empty($param['member_withdraw_scene'])){
            foreach($config[$param['member_withdraw_scene']]['infos'] as $k=>$v){
                $v['info_content'] = $param['member_withdraw_'.$k] ?? '';
                $data['member_withdraw_info'][$k] = $v;
            }
        }

        return $data;
    }
}